package com.bpmn.utils;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.UserTask;
import org.flowable.engine.impl.bpmn.behavior.MultiInstanceActivityBehavior;

/**
 * 任务配置类
 *
 * @author wangzongrun
 */
@Slf4j
public class TaskConfigUtils {

    /**
     * 从BPMN模型中解析任务配置（扩展属性）
     */
    public static TaskConfig getTaskConfig(UserTask userTask) {
        try {
            // 1. 提取扩展属性
            String formFieldPermissions = ModelUtils.getUserTaskExtension(userTask, "formFieldPermissions");
            String operationPermissions = ModelUtils.getUserTaskExtension(userTask, "operationPermissions");
            String approvalMode = ModelUtils.getUserTaskExtension(userTask, "approvalMode");
            String emptyAssigneeStrategy = ModelUtils.getUserTaskExtension(userTask, "emptyAssigneeStrategy");
            String defaultAssignee = ModelUtils.getUserTaskExtension(userTask, "defaultAssignee");
            String autoApproveSettings = ModelUtils.getUserTaskExtension(userTask, "autoApproveSettings");

            //是多实例节点
            boolean multiInstanceNode = userTask.getBehavior() instanceof MultiInstanceActivityBehavior;

            // 2. 提取Flowable原有属性
            String assignee = userTask.getAssignee();
            String candidateUsers = String.join(",", userTask.getCandidateUsers());
            String candidateGroups = String.join(",", userTask.getCandidateGroups());
            String dueDate = userTask.getDueDate();
            String formKey = userTask.getFormKey();

            // 3. 构建TaskConfig对象
            return new TaskConfig(formFieldPermissions, operationPermissions, approvalMode,
                    emptyAssigneeStrategy, defaultAssignee, autoApproveSettings, multiInstanceNode,
                    assignee, candidateUsers, candidateGroups, dueDate, formKey);
        } catch (Exception e) {
            log.error("解析BPMN模型获取任务配置失败： taskDefinitionId={}", userTask.getId(), e);
            return TaskConfig.EMPTY_CONFIG;
        }
    }

    /**
     * 任务配置模型（封装扩展属性，避免Map传递的混乱）
     */
    @Data
    public static class TaskConfig {
        // EMPTY配置实例
        public static final TaskConfig EMPTY_CONFIG = new TaskConfig();

        // 表单字段权限
        private String formFieldPermissions;

        // 操作权限
        private String operationPermissions;

        // 审批模式
        private String approvalMode;

        // 空审批人策略
        private String emptyAssigneeStrategy;

        // 默认审批人
        private String defaultAssignee;

        // 自动审批设置
        private String autoApproveSettings;

        //是否是多实例节点
        boolean multiInstanceNode;

        // Flowable原有属性
        private String assignee;
        private String candidateUsers;
        private String candidateGroups;
        private String dueDate;
        private String formKey;

        public TaskConfig() {
        }

        public TaskConfig(String formFieldPermissions, String operationPermissions,
                          String approvalMode, String emptyAssigneeStrategy,
                          String defaultAssignee, String autoApproveSettings,
                          boolean multiInstanceNode,
                          String assignee, String candidateUsers, String candidateGroups,
                          String dueDate, String formKey) {
            this.formFieldPermissions = formFieldPermissions;
            this.operationPermissions = operationPermissions;
            this.approvalMode = approvalMode;
            this.emptyAssigneeStrategy = emptyAssigneeStrategy;
            this.defaultAssignee = defaultAssignee;
            this.autoApproveSettings = autoApproveSettings;
            this.multiInstanceNode = multiInstanceNode;
            this.assignee = assignee;
            this.candidateUsers = candidateUsers;
            this.candidateGroups = candidateGroups;
            this.dueDate = dueDate;
            this.formKey = formKey;
        }
    }

}